package com.powernode.controller;

import com.powernode.domain.Member;
import com.powernode.model.Result;
import com.powernode.service.MemberService;
import com.powernode.util.AuthUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物系统：会员控制层
 */
@Api(tags = "购物系统会员接口管理")
@RequestMapping("p/user")
@RestController
public class MemberController {

    @Autowired
    private MemberService memberService;

    @ApiOperation("更新用户的头像和昵称")
    @PutMapping("setUserInfo")
    public Result<String> modifyUserInfo(@RequestBody Member member) {
        Long userId = AuthUtils.getLoginUserId();
        member.setId(userId);
        member.setSex(member.getSex().equals(0)?"M":"F");
        boolean updated = memberService.updateById(member);
        return Result.handle(updated);
    }

//    p/user/isBindPhone
    @ApiOperation("查询用户是否绑定手机号码")
    @GetMapping("isBindPhone")
    public Result<Boolean> loadUserIsBindPhone() {
        Long userId = AuthUtils.getLoginUserId();
        Member member = memberService.getById(userId);
        return Result.success(StringUtils.hasText(member.getUserMobile()));
    }

    //////////////////////// feign 接口 /////////////////////////////////////
    @ApiOperation("根据会员openIds查询会员对象集合")
    @GetMapping("getMemberListByOpenIds")
    public Result<List<Member>> getMemberListByOpenIds(@RequestParam List<String> openIds) {
        List<Member> list = memberService.queryMemberListByOpenIds(openIds);
        return Result.success(list);
    }

    @ApiOperation("查询用户的详细信息")
    @GetMapping("getMemberInfo/{id}")
    public Result<Member> getMemberInfo(@PathVariable("id") String id) {
        Member member = memberService.queryMemberByOpenId(id);
        return Result.success(member);
    }
}
